package org.inspiration.dudu.web.controller.user;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.catalina.User;
import org.inspiration.dudu.common.pojo.Result;
import org.inspiration.dudu.user.model.dto.UserFollowDTO;
import org.inspiration.dudu.user.model.vo.UserVO;
import org.inspiration.dudu.web.annotation.Loggable;
import org.inspiration.dudu.web.service.user.UserFollowFacadeService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user-follow")
@Tag(name = "用户关注管理接口") // Updated tag for clarity
@RequiredArgsConstructor
@Loggable

public class UserFollowController {
    private final UserFollowFacadeService userFollowFacadeService;

    /**
     * 关注某个用户
     *
     * @param userFollowDTO 用户关注信息
     * @return 操作结果信息
     */
    @PostMapping
    @Operation(summary = "关注用户", description = "关注指定的用户") // Added description
    public Result<Void> followUser(@RequestBody UserFollowDTO userFollowDTO) {
        userFollowFacadeService.followUser(userFollowDTO);
        return Result.success();
    }

    /**
     * 取消关注某个用户
     *
     * @param userFollowDTO 用户关注信息
     * @return 操作结果信息
     */
    @DeleteMapping
    @Operation(summary = "取消关注", description = "取消对指定用户的关注") // Added description
    public Result<Void> unfollowUser(@RequestBody UserFollowDTO userFollowDTO) {
        userFollowFacadeService.unfollowUser(userFollowDTO);
        return Result.success();
    }



    /**
     * 获取某个用户关注的所有用户 ID 列表
     *
     * @param followerId 关注者的用户 ID
     * @return 被关注者的用户 ID 列表
     */
    @GetMapping("/following")
    @Operation(summary = "获取关注列表", description = "获取指定用户关注的所有用户(分页)") // Added description
    public Result<List<UserVO>> getFollowingUsers(@RequestParam("followerId") Long followerId,
                                                  @RequestParam("pageNum") Integer pageNum,
                                                  @RequestParam("pageSize") Integer pageSize) {
        List<UserVO> followingUsers = userFollowFacadeService.getFollowingUsers(followerId, pageNum, pageSize);
        return Result.success(followingUsers);
    }



    /**
     * 获取某个用户的所有粉丝的用户 ID 列表
     *
     * @param followingId 被关注者的用户 ID
     * @return 粉丝的用户 ID 列表
     */
    @GetMapping("/followers")
    @Operation(summary = "获取粉丝列表", description = "获取指定用户的所有粉丝的用户(分页)") // Added description
    public Result<List<UserVO>> getFollowerUsers(@RequestParam("followingId") Long followingId,
                                                 @RequestParam("pageNum") Integer pageNum,
                                                 @RequestParam("pageSize") Integer pageSize) {
        List<UserVO> followerUsers = userFollowFacadeService.getFollowerUsers(followingId, pageNum, pageSize);
        return Result.success(followerUsers);
    }

    /**
     * 判断一个用户是否关注了另一个用户
     *
     * @param followerId 关注者的用户 ID
     * @param followingId 被关注者的用户 ID
     * @return 如果关注返回 true，否则返回 false
     */
    @GetMapping("/is-following")
    @Operation(summary = "判断是否关注", description = "检查一个用户是否关注了另一个用户") // Added description
    public Result<Boolean> isFollowing(@RequestParam("followerId") Long followerId,
                                       @RequestParam("followingId") Long followingId) {
        Boolean isFollowing = userFollowFacadeService.isFollowing(followerId, followingId);
        return Result.success(isFollowing);
    }
}
